﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using CE.User;
using CE.Global;
using CE.Provider;

public partial class Provider_CarInsurance_ProviderCarNCDCapacity : System.Web.UI.Page
{
    string PageName = "CXCI";
    string SubPageName = "CXCINCDVHCAP";
    public static Int64 _providercarncdcapid = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null || Session[CE.Global.General.ProviderCompanyId] == null)
        {
            Response.Redirect("~/Provider/ProviderSecureLogin.aspx");
        }
        CheckMainPageAccess();
        CheckSubPageAccess();

        if (!IsPostBack)
        {
            usrEffectivityDate.StartYear = CE.Global.General.GetUTCDateTime().Year;
            usrEffectivityDate.EndYear = CE.Global.General.GetUTCDateTime().Year + 2;

            ClearProcess();
            if (Request.Params["PCNCDCId"] != null)
            {
                Int64 _carncdcapacityid;
                if (Int64.TryParse(Request.Params["PCNCDCId"].ToString(), out _carncdcapacityid))
                {
                    ShowCarNCDCapacity(_carncdcapacityid);
                }
            }
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            string _authcode = CE.Security.MySecurity.Encrypt(txtAuthCode.Text, true);
            if (CheckAuthCode(_authcode, 100))
            {
                CarNCDCapacity _carncdcapacity = new CarNCDCapacity();
                CarNCDCapacityDB _carncdcapacitydb = new CarNCDCapacityDB();

                _carncdcapacity.ProviderCarNCDCapId = _providercarncdcapid;
                int _trantype = 1;
                
                if (_carncdcapacity.ProviderCarNCDCapId > 0)
                {
                    _trantype = 2;
                }
                _carncdcapacity.ProviderCompanyId = Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId].ToString());
                _carncdcapacity.NCDPercent = ddlNCDPercent.SelectedValue.ToString();
                _carncdcapacity.CarCapacity = ddlCarCapacity.SelectedValue.ToString();
                _carncdcapacity.Rate = Convert.ToSingle(txtRate.Text.Trim());
                _carncdcapacity.RefCEUserId = Convert.ToInt64(Session[CE.Global.General.UserId].ToString());
                if (chkStatus.Checked == true)
                {
                    _carncdcapacity.Status = true;
                }
                else if (chkStatus.Checked == false)
                {
                    _carncdcapacity.Status = false;
                }

                if (rdoEffectivity.SelectedValue == "Yes" && usrEffectivityDate.GetDate() > CE.Global.General.GetUTCDateTime())
                {
                    _carncdcapacity.FInfo1 = float.Parse(txtNewRate.Text);
                    _carncdcapacity.DInfo1 = usrEffectivityDate.GetDate();

                }
                else
                {
                    _carncdcapacity.FInfo1 = float.Parse(txtRate.Text);
                    _carncdcapacity.DInfo1 = CE.Global.General.GetUTCDateTime();

                }

                _carncdcapacity.ProviderCarNCDCapId = _carncdcapacitydb.Insert(_carncdcapacity);

                if (_carncdcapacity.ProviderCarNCDCapId > 0)
                {
                    InsertProviderAuthTransaction(_carncdcapacity.ProviderCarNCDCapId, "CICarGender", _trantype, 100, _authcode, _carncdcapacity.ProviderCompanyId, _carncdcapacity.RefCEUserId);
                    ProviderUpdateSave(_carncdcapacity.ProviderCarNCDCapId);
                    ClearProcess();
                    lblMessage.Text = "Saved successfully!";
                    Response.Redirect("ViewProviderCarNCDCapacity.aspx");
                }
                else
                {
                    lblError.Text = "Saving failed!";
                }
            }
            else
            {
                lblMessage.Text = "Wrong auth code!";
            }
        }
    }

    private void ProviderUpdateSave(Int64 ProviderRefId)
    {
        ProviderUpdates _providerupdates = new ProviderUpdates();
        ProviderUpdatesDB _providerupdatesDB = new ProviderUpdatesDB();

        _providerupdates.UpdateId = 0;
        _providerupdates.CXTableId = GetCXTableId();
        _providerupdates.ProviderRefId = ProviderRefId;

        if (rdoEffectivity.SelectedValue == "Yes" && usrEffectivityDate.GetDate() > CE.Global.General.GetUTCDateTime())
        {
            //Scheduled
            _providerupdates.UpdateType = 1;
        }
        else
        {
            //Immediate
            _providerupdates.UpdateType = 0;
        }

        _providerupdates.ProviderCompanyId = Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId]);
        _providerupdates.RefCEUserID = Convert.ToInt64(Session[General.UserId].ToString());

        _providerupdatesDB.Insert(_providerupdates);

    }

    private Int32 GetCXTableId()
    {
        Int32 CXID = 0;

        ProviderTableListings _tablelist = new ProviderTableListings();
        ProviderTableListingsDB _tablelistdb = new ProviderTableListingsDB();

        int ProviderType = 100; //Car Insurance
        string CXTable = "tblProviderCarNCDCapacity";

        _tablelist = _tablelistdb.GetProviderTableListingsSingleItem(ProviderType, Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId]), CXTable);

        if (_tablelist != null)
            CXID = _tablelist.CXTableId;

        return CXID;

    }

    protected void btnClearForm_Click(object sender, EventArgs e)
    {
        ClearProcess();
    }

    protected void ibtBack_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("ViewProviderCarNCDCapacity.aspx");
    }

    protected void ddlNCDPercent_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlNCDPercent.SelectedIndex == 0)
        {
            ClearProcess();
        }
        else if (ddlNCDPercent.SelectedIndex > 0)
        {
            _providercarncdcapid = 0;
            ddlCarCapacity.SelectedIndex = 0;
            txtRate.Text = "";
            chkStatus.Checked = true;
            lblMessage.Text = "";
            lblError.Text = "";
            //btnSubmit.Text = "Submit";
            //btnClearForm.Text = "Clear";
        }
    }

    protected void ddlCarCapacity_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlCarCapacity.SelectedIndex > 0)
        {
            CarNCDCapacity _carncdcapacity = new CarNCDCapacity();
            CarNCDCapacityDB _carncdcapacitydb = new CarNCDCapacityDB();
            _carncdcapacity = _carncdcapacitydb.GetCarNCDCapacity(Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId].ToString()), ddlNCDPercent.SelectedValue, ddlCarCapacity.SelectedValue);
            if (_carncdcapacity != null)
            {
                _providercarncdcapid = Convert.ToInt64(_carncdcapacity.ProviderCarNCDCapId.ToString());
                txtRate.Text = _carncdcapacity.Rate.ToString();
                if (_carncdcapacity.Status == true)
                {
                    chkStatus.Checked = true;
                }
                else
                {
                    chkStatus.Checked = false;
                }

                if (_carncdcapacity.DInfo1 > CE.Global.General.GetUTCDateTime())
                {
                    divNewRate.Visible = true;
                    txtNewRate.Text = _carncdcapacity.FInfo1.ToString();
                    rdoEffectivity.SelectedValue = "Yes";
                }
                else
                {
                    divNewRate.Visible = false;
                    rdoEffectivity.SelectedValue = "No";
                }

                usrEffectivityDate.Day = _carncdcapacity.DInfo1.Day;
                usrEffectivityDate.Month = _carncdcapacity.DInfo1.Month;
                usrEffectivityDate.Year = _carncdcapacity.DInfo1.Year;
                //btnSubmit.Text = "Update";
                //btnClearForm.Text = "Add New";
            }
            else
            {
                _providercarncdcapid = 0;
                txtRate.Text = "";
                chkStatus.Checked = true;
                lblMessage.Text = "";
                lblError.Text = "";
                //btnSubmit.Text = "Submit";
                //btnClearForm.Text = "Clear";
            }
        }
    }

    private void ShowCarNCDCapacity(Int64 ProviderCarNCDCapId)
    {
        CarNCDCapacity _carncdcapacity = new CarNCDCapacity();
        CarNCDCapacityDB _carncdcapacitydb = new CarNCDCapacityDB();
        _carncdcapacity = _carncdcapacitydb.GetCarNCDCapacity(ProviderCarNCDCapId);
        if (_carncdcapacity != null)
        {
            _providercarncdcapid = Convert.ToInt64(_carncdcapacity.ProviderCarNCDCapId.ToString());
            ddlNCDPercent.SelectedValue = _carncdcapacity.NCDPercent.ToString();
            ddlCarCapacity.SelectedValue = _carncdcapacity.CarCapacity.ToString();
            txtRate.Text = _carncdcapacity.Rate.ToString();
            if (_carncdcapacity.Status == true)
            {
                chkStatus.Checked = true;
            }
            else
            {
                chkStatus.Checked = false;
            }

            if (_carncdcapacity.DInfo1 > CE.Global.General.GetUTCDateTime())
            {
                divNewRate.Visible = true;
                txtNewRate.Text = _carncdcapacity.FInfo1.ToString();
                rdoEffectivity.SelectedValue = "Yes";
            }
            else
            {
                divNewRate.Visible = false;
                rdoEffectivity.SelectedValue = "No";
            }

            usrEffectivityDate.Day = _carncdcapacity.DInfo1.Day;
            usrEffectivityDate.Month = _carncdcapacity.DInfo1.Month;
            usrEffectivityDate.Year = _carncdcapacity.DInfo1.Year;
            //btnSubmit.Text = "Update";
            //btnClearForm.Text = "Add New";
        }
    }

    public void ClearProcess()
    {
        _providercarncdcapid = 0;
        ddlNCDPercent.SelectedIndex = 0;
        ddlCarCapacity.SelectedIndex = 0;
        txtRate.Text = "";
        chkStatus.Checked = true;
        lblMessage.Text = "";
        lblError.Text = "";
        //btnSubmit.Text = "Submit";
        //btnClearForm.Text = "Clear";
    }

    public void EffectiveWhen(object sender, EventArgs e)
    {
        if (rdoEffectivity.SelectedValue == "Yes")
        {
            divNewRate.Visible = true;
        }
        else
        {
            divNewRate.Visible = false;
        }

        usrEffectivityDate.Day = CE.Global.General.GetUTCDateTime().Day;
        usrEffectivityDate.Month = CE.Global.General.GetUTCDateTime().Month;
        usrEffectivityDate.Year = CE.Global.General.GetUTCDateTime().Year;
    }

    protected bool CheckAuthCode(string strAuthCode, int intAuthCodeType)
    {
        bool _retValue = false;
        ProviderAuthCode _authcode = new ProviderAuthCode();
        ProviderAuthCodeDB _authcodedb = new ProviderAuthCodeDB();
        _authcode = _authcodedb.GetProviderAuthCode(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()), intAuthCodeType, strAuthCode);
        if (_authcode != null)
        {
            _retValue = true;
        }

        return _retValue;
    }

    protected Int64 InsertProviderAuthTransaction(Int64 TransactionId, string TransactionCode, int TransactionType,
        int AuthCodeType, string AuthCode, Int64 ProviderCompanyId, Int64 RefCEUserId)
    {
        ProviderAuthTransaction _authtrans = new ProviderAuthTransaction();
        ProviderAuthTransactionDB _authtransdb = new ProviderAuthTransactionDB();

        _authtrans.TransactionId = TransactionId;
        _authtrans.TransactionCode = TransactionCode;
        _authtrans.TransactionType = TransactionType;
        _authtrans.AuthCode = AuthCode;
        _authtrans.AuthCodeType = AuthCodeType;
        _authtrans.ProviderCompanyId = ProviderCompanyId;
        _authtrans.RefCEUserID = RefCEUserId;

        _authtrans.AuthTransId = _authtransdb.Insert(_authtrans);
        return _authtrans.AuthTransId;
    }

    private void CheckMainPageAccess()
    {
        Int64 PageAccess = General.GetProviderMainPageAccess(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()),
                                                        Convert.ToInt64(Session[General.UserId].ToString()), PageName);
        if (PageAccess == 0)
        {
            //Response.Redirect("~/Provider/UserMessage.aspx?errorcode=2000");
            Response.Redirect("~/Provider/UserMessage.aspx?mt=UM&ec=2000");
        }
    }

    private void CheckSubPageAccess()
    {
        Int64 PageAccess = General.GetProviderSubPageAccess(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()),
                                                        Convert.ToInt64(Session[General.UserId].ToString()), SubPageName);
        if (PageAccess == 0)
        {
            //Response.Redirect("~/Provider/UserMessage.aspx?errorcode=2000");
            Response.Redirect("~/Provider/UserMessage.aspx?mt=UM&ec=2000");
        }
        else if (PageAccess == 100)
        {
            btnSubmit.Enabled = false;
            btnClearForm.Enabled = false;
            //btnDelete.Enabled = false;
        }
        else if (PageAccess == 200)
        {
            btnSubmit.Enabled = true;
            btnClearForm.Enabled = true;
            //btnDelete.Enabled = false;
        }
        else if (PageAccess == 300)
        {
            btnSubmit.Enabled = true;
            btnClearForm.Enabled = true;
            //btnDelete.Enabled = true;
        }
    }

}
